package com.chinasoft.reposearch.service.impl;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.chinasoft.reposearch.entity.ResponseResult;
import com.chinasoft.reposearch.entity.dto.TopicCountDto;
import com.chinasoft.reposearch.entity.po.TopicCount;
import com.chinasoft.reposearch.mapper.TopicCountMapper;
import com.chinasoft.reposearch.service.TopicCountService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class TopicCountServiceImpl implements TopicCountService {
    private final TopicCountMapper topicCountMapper;
//    @Autowired
//    public TopicCountServiceImpl(TopicCountMapper topicCountMapper) {
//        this.topicCountMapper = topicCountMapper;
//    }

    /**
     * 查询所有主题 某一年月的 列表和数量
     */
    @Override
    public ResponseResult getTopicCounts(Integer year, Integer month) {
//        if(year == null&& month!=null){
//            return ResponseResult.success(null);
//        }
//
//        List<TopicCount> topicCountList=null;
//        if(year != null&&month==null){
//            topicCountList=topicCountMapper.selectList(
//                    Wrappers.<TopicCount>lambdaQuery()
//                            .eq(TopicCount::getYear, year)
//            );
//        } else if(year != null&&month!=null){
//            topicCountList = topicCountMapper.selectList(
//                    Wrappers.<TopicCount>lambdaQuery()
//                            .eq(TopicCount::getYear, year)
//                            .eq(TopicCount::getMonth, month)
//            );
//        }else{
//            topicCountList = topicCountMapper.selectList(
//                    Wrappers.<TopicCount>lambdaQuery()
//            );
//        }

        List<TopicCount> TopicCountList = topicCountMapper.selectList(
                Wrappers.<TopicCount>lambdaQuery()
                        // 仅当year不为null时，才添加year条件
                        .eq(year != null, TopicCount::getYear, year)
                        // 仅当month不为null时，才添加month条件
                        .eq(month != null, TopicCount::getMonth, month)
        );

        TopicCountDto topicCountDto = new TopicCountDto(TopicCountList.size(), TopicCountList);
        return ResponseResult.success(topicCountDto);
    }

    /**
     * GET /topic-trending?topic=xxxx&year=yyyy
     * 查询某一主题 在所有年月的 列表和数量
     */
    @Override
    public List<TopicCount> getTopicTrending(String topic, Integer year) {
        List<TopicCount> topicTrendingList = topicCountMapper.selectList(
                Wrappers.<TopicCount>lambdaQuery()
                        .eq(TopicCount::getTopic, topic)
                        .eq(TopicCount::getYear, year)
        );
        TopicCountDto topicCountDto = new TopicCountDto(topicTrendingList.size(), topicTrendingList);
        return topicTrendingList;
    }

    @Override
    public List<String> getTopics() {
        List<TopicCount> list = topicCountMapper.selectList(
                Wrappers.<TopicCount>lambdaQuery()
                        .select(TopicCount::getId, TopicCount::getTopic)
                        .groupBy(TopicCount::getTopic)
        );
        return list.stream().map(TopicCount::getTopic).toList();
    }

    @Override
    public List<TopicCount> getTopicTrendingYears(String topic, Integer start, Integer end) {
        return topicCountMapper.selectListYears(topic, start, end);
    }
}
